﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.IO;
using DTO;
using DAO;
using BUS;

namespace Cucre
{
    public partial class WebForm4 : System.Web.UI.Page
    {
        private static MEMBER member;

        public static MEMBER Member
        {
            get { return WebForm4.member; }
            set { WebForm4.member = value; }
        }
        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);
            string path = Server.MapPath("theme.txt");
            StreamReader SR = new StreamReader(path);
            string nameTheme = SR.ReadLine();
            SR.Close();
            base.OnPreInit(e);
            if (Session["MyTheme"] == null)
            {
                Session.Add("MyTheme", nameTheme);
                Page.Theme = (string)Session["MyTheme"];
            }
            else
            {
                Page.Theme = (string)Session["MyTheme"];
            }
            Session.Add("user_login", null);

        }
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                member = new MEMBER();
            }
            catch
            {
            }
            if (user_name.Text.Trim() == null || user_name.Text.Trim() == "")
            {
                ErrorFullName.Visible = true;
                user_name.BorderColor = System.Drawing.Color.Red;
            }
            else
                if (user_email.Text.Trim() == null || user_email.Text.Trim() == "" || !user_email.Text.Trim().Contains(".") || !user_email.Text.Trim().Contains("@"))
                {
                    ErrorFullName.Visible = false;
                    user_name.BorderColor = System.Drawing.Color.Black;
                    ErrorMail.Visible = true;
                    user_email.BorderColor = System.Drawing.Color.Red;
                }
                else
                    if (user_login.Text.Trim() == null || user_login.Text.Trim() == "")
                    {
                        ErrorMail.Visible = false;
                        user_email.BorderColor = System.Drawing.Color.Black;
                        ErrorUsername.Visible = true;
                        user_login.BorderColor = System.Drawing.Color.Red;
                    }
                    else
                        if (user_password.Text.Trim() == null || user_password.Text.Trim() == "")
                        {
                            ErrorUsername.Visible = false;
                            user_login.BorderColor = System.Drawing.Color.Black;
                            ErrorPassword.Visible = true;
                            user_password.BorderColor = System.Drawing.Color.Red;
                        }
                        else
                            if (confim_password.Text.Trim() == null || confim_password.Text.Trim() == "" || confim_password.Text.Trim() != user_password.Text.Trim())
                            {
                                ErrorPassword.Visible = false;
                                user_password.BorderColor = System.Drawing.Color.Black;
                                ErrorConfimPassword.Visible = true;
                                confim_password.BorderColor = System.Drawing.Color.Red;
                            }
                            else
                                if (user_address.Text.Trim() == null || user_address.Text.Trim() == "")
                                {
                                    ErrorConfimPassword.Visible = false;
                                    confim_password.BorderColor = System.Drawing.Color.Black;
                                    ErrorAddress.Visible = true;
                                    user_address.BorderColor = System.Drawing.Color.Red;
                                }
                                else
                                    if (user_phone.Text.Trim() == null || user_phone.Text.Trim() == "")
                                    {
                                        ErrorAddress.Visible = false;
                                        user_address.BorderColor = System.Drawing.Color.Black;
                                        ErrorPhone.Visible = true;
                                        user_phone.BorderColor = System.Drawing.Color.Red;
                                    }
                                    else
                                        if (user_identity.Text.Trim() == null || user_identity.Text.Trim() == "" || confim_password.Text.Trim() != user_password.Text.Trim())
                                        {
                                            ErrorPhone.Visible = false;
                                            user_phone.BorderColor = System.Drawing.Color.Black;
                                            ErrorCMND.Visible = true;
                                            user_identity.BorderColor = System.Drawing.Color.Red;
                                        }
                                        else
                                            if (capchar.Text.Trim() == null || capchar.Text.Trim() == "")
                                            {
                                                ErrorCMND.Visible = false;
                                                user_identity.BorderColor = System.Drawing.Color.Black;
                                                ErrorCapchar.Visible = true;
                                                capchar.BorderColor = System.Drawing.Color.Red;
                                            }
                                            else
                                                if (!CheckBox1.Checked)
                                                {
                                                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "clientScript", "alert('Bạn chưa đồng ý gia nhập cucre.vn", true);
                                                }
                                                else
                                                {
                                                    ccJoin.ValidateCaptcha(capchar.Text);
                                                    if (!ccJoin.UserValidated)
                                                    {
                                                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "clientScript", "alert('Mã bảo vệ không đúng')", true);
                                                    }
                                                    else
                                                    {
                                                        ErrorFullName.Visible = false;
                                                        user_name.BorderColor = System.Drawing.Color.Black;
                                                        ErrorMail.Visible = false;
                                                        user_email.BorderColor = System.Drawing.Color.Black;
                                                        ErrorUsername.Visible = false;
                                                        user_login.BorderColor = System.Drawing.Color.Black;
                                                        ErrorPassword.Visible = false;
                                                        user_password.BorderColor = System.Drawing.Color.Black;
                                                        ErrorConfimPassword.Visible = false;
                                                        confim_password.BorderColor = System.Drawing.Color.Black;
                                                        ErrorAddress.Visible = false;
                                                        user_address.BorderColor = System.Drawing.Color.Black;
                                                        ErrorPhone.Visible = false;
                                                        user_phone.BorderColor = System.Drawing.Color.Black;
                                                        ErrorCMND.Visible = false;
                                                        user_identity.BorderColor = System.Drawing.Color.Black;

                                                        member.ACCOUNT = new ACCOUNT();
                                                        member.ACCOUNT.FULLNAME = user_name.Text.Trim();
                                                        member.ACCOUNT.EMAIL = user_email.Text.Trim();
                                                        member.ACCOUNT.USERNAME = user_login.Text.Trim();
                                                        member.ACCOUNT.PASSWORD = user_password.Text.Trim();
                                                        member.ACCOUNT.ADDRESS = user_address.Text.Trim();
                                                        member.ACCOUNT.PHONE_NUMBER = user_phone.Text.Trim();
                                                        member.ACCOUNT.CMND = user_identity.Text.Trim();
                                                        
                                                        if (MemberBUS.insertMember(member) == true)
                                                        {
                                                            Session.Add("user_login", member);
                                                            Session.Add("user_style", "3");
                                                            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "clientScript", "alert('Bạn đã đăng kí thành công');window.location.href='index.aspx'", true);
                                                        }
                                                        else
                                                        {
                                                            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "clientScript", "alert('Đăng kí thất bại. Tài khoản " + user_login.Text.Trim() + " đã có người sử dụng" + "');", true);

                                                        }
                                                    }
                                                }
        }
    }
}
